/*
 * @(#) ArticleContoller.java 2018年2月8日 下午5:09:49
 *
 * Copyright 2018 CIMIP, Inc. All rights reserved.
 * H-VISIONS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package cn.hc.share.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hc.share.entity.Article;
import cn.hc.share.entity.ArticleMsg;
import cn.hc.share.entity.Comment;
import cn.hc.share.service.ArticleAndTagSaveException;
import cn.hc.share.service.ArticleNotFindException;
import cn.hc.share.service.ArticleSaveException;
import cn.hc.share.service.ArticleService;
import cn.hc.share.service.UserNotFoundException;
import cn.hc.share.util.JsonResult;

@Controller
@RequestMapping("/article")
public class ArticleContoller extends AbstractController
{

	@Resource
	private ArticleService articleService;

	// 访问量增加
	@RequestMapping("/statistCommentsNum.do")
	@ResponseBody
	public Object statistCommentsNum(String articleId)
	{

		int num = articleService.statistCommentsNum(articleId);
		Object obj = num;
		return new JsonResult(obj);

	}
	
	// 访问量增加
	@RequestMapping("/articleVisistAdd.do")
	@ResponseBody
	public Object articleVisistAdd(String articleId)
	{
		
		int num = articleService.addVisitArticle(articleId);
		Object obj = num;
		return new JsonResult(obj);
		
	}

	//保存文章
	@RequestMapping("/saveArticle.do")
	@ResponseBody
	public Object saveArticle(String articleId, String authorId, String title, String cont, String menuId,
			String tagIds, String category)
	{

		Article article = articleService.saveArticle(articleId, authorId, title, cont, menuId, tagIds, category);
		return new JsonResult(article);
	}

	@RequestMapping("/countArticleMsg.do")
	@ResponseBody
	public Object countArticleMsg(String articleId)
	{

		Map<String, Integer> count = articleService.countArticleMsg(articleId);
		return new JsonResult(count);

	}

	// 获取草稿
	@RequestMapping("/checkDraft.do")
	@ResponseBody
	public Object checkDraft(String authorId)
	{
		Article draft = articleService.findDraft(authorId);
		return new JsonResult(draft);
	}

	// 获取某一篇文章
	@RequestMapping("/getArticleMsg.do")
	@ResponseBody
	public Object getArticleMsg(String id)
	{

		Article article = articleService.getArticleById(id);
		return new JsonResult(article);
	}

	// 获取某一篇文章收藏/有用/无用信息
	@RequestMapping("/updateArticleMsgSuspend.do")
	@ResponseBody
	public Object updateArticleMsgSuspend(String articleId, String author, String collectState, String usefullState)
	{
		System.out.println("1:" + articleId + ";2:" + author + ";3:" + collectState + ";4:" + usefullState);
		ArticleMsg articleMsg = articleService.updateArticleMsg(articleId, author, collectState, usefullState);
		return new JsonResult(articleMsg);
	}

	// 获取某一篇文章收藏/有用/无用信息
	@RequestMapping("/getArticleMsgSuspend.do")
	@ResponseBody
	public Object getArticleMsgSuspend(String articleId, String author)
	{
		System.out.println("1:" + articleId + ";2:" + author);
		ArticleMsg articleMsg = articleService.getArticleMsgSuspend(articleId, author);
		return new JsonResult(articleMsg);
	}

	// 获取文章
	@RequestMapping("/getArticles.do")
	@ResponseBody
	public Object getArticles(String menuId, String tagId, String title, String page, String pageSize)
	{
		System.out.println(
				"menuId:" + menuId + ";tagId:" + tagId + ";title:" + title + ";page:" + page + ";pageSize:" + pageSize);
		List<Article> articles = articleService.getArticles(menuId, tagId, title, page, pageSize);
		return new JsonResult(articles);
	}

	// 获取文章总数
	@RequestMapping("/getArticlesCount.do")
	@ResponseBody
	public Object getArticlesCount(String menuId, String tagId, String title)
	{
		System.out.println("menuId:" + menuId + ";tagId:" + tagId + ";title:" + title);
		int totalCount = articleService.getArticlesCount(menuId, tagId, title);
		Object obj = totalCount;
		return new JsonResult(obj);
	}

	@ExceptionHandler(ArticleSaveException.class)
	@ResponseBody
	public JsonResult handleArtricleSave(ArticleSaveException e)
	{
		e.printStackTrace();
		return new JsonResult(2, e);
	}
	@ExceptionHandler(ArticleAndTagSaveException.class)
	@ResponseBody
	public JsonResult handleArtricleAndTagSave(ArticleSaveException e)
	{
		e.printStackTrace();
		return new JsonResult(5, e);
	}

	@ExceptionHandler(ArticleNotFindException.class)
	@ResponseBody
	public JsonResult handleArtricleFind(ArticleSaveException e)
	{
		e.printStackTrace();
		return new JsonResult(3, e);
	}

	@ExceptionHandler(UserNotFoundException.class)
	@ResponseBody
	public JsonResult handleUserNotFound(UserNotFoundException e)
	{
		e.printStackTrace();
		return new JsonResult(2, e);
	}

}
